﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DofusPlayer.Anm2;

namespace DofusPlayer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            //Cracking the ANM file type

            Anm m_anm = new Anm();

            BinaryReader m_reader = new BinaryReader(File.Open("resource/40.anm", FileMode.Open));


            //Load Version, frameCount, frameRate
            byte version = m_reader.ReadByte();
            short frameCount = m_reader.ReadInt16();
            float frameRate = m_reader.ReadByte();

            m_anm.m_header.init(version, frameCount, frameRate);

            //Load Texture Info
            int numTextures = m_reader.ReadInt16() & 0xffff;
            m_anm.m_texturesInfo = new AnmTextureInfo[numTextures];
            for (int i = 0; i < numTextures; i++)
            {
                m_anm.m_texturesInfo[i] = new AnmTextureInfo();

                string name = ReadString(m_reader);
                

                //Loading CRC
                int crc = m_reader.ReadInt32();

                //m_anm.m_texturesInfo[i].init(name, crc);
            }


            //Load Shape Definition
            int numShapes = m_reader.ReadInt16() & 0xffff;
            m_anm.m_shapeDefinitionById = new Dictionary<int, AnmShapeDefinition>();
            for (int i = 0; i < numShapes; i++)
            {
                AnmShapeDefinition asd = new AnmShapeDefinition();

                asd.m_id = m_reader.ReadInt16();
                asd.m_textureIndex = m_reader.ReadInt16();

                asd.m_top = (m_reader.ReadInt16() & 0xffff) / 65535f;
                asd.m_left = (m_reader.ReadInt16() & 0xffff) / 65535f;
                asd.m_bottom = (m_reader.ReadInt16() & 0xffff) / 65535f;
                asd.m_right = (m_reader.ReadInt16() & 0xffff) / 65535f;

                asd.m_width = m_reader.ReadInt16();
                asd.m_height = m_reader.ReadInt16();

                asd.m_offsetX = m_reader.ReadSingle();
                asd.m_offsetY = m_reader.ReadSingle();

                m_anm.m_shapeDefinitionById.Add(asd.m_id, asd);
            }

            //Load Sprite
            int numSprites = m_reader.ReadInt16() & 0xffff;
            m_anm.m_spritDefinitionsbyId = new Dictionary<int,AnmSpriteDefinition>();
            for (int i = 0; i < numSprites; i++ )
            {
                AnmSpriteDefinition asd = new AnmSpriteDefinition();

                asd.m_id = m_reader.ReadInt16();
                asd.m_flags = m_reader.ReadByte();
                if ((asd.m_flags & 0x40) != 0)
                {
                    asd.m_name = ReadString(m_reader);
                }
                //asd.m_nameCRC = 
            }

        }

        private static String ReadString(BinaryReader m_reader)
        {
            //Loading Name
            Byte[] ba = new Byte[100];
            Byte bitBuff = m_reader.ReadByte();
            int count = 0;

            while (bitBuff != 0)
            {
                ba[count] = bitBuff;
                count++;
                bitBuff = m_reader.ReadByte();
            }
            if (count > 100) throw new IOException("ANM Name length > 100");
            
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            String name = enc.GetString(ba).Trim();
            return name;
        }
    }
}
